package model;
import java.net.UnknownHostException;
import java.util.Date;

import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.workplicity.entry.Entry;
import org.workplicity.util.Helper;
import org.workplicity.util.MongoHelper;
import org.workplicity.worklet.WorkletContext;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

import cc.test.bridge.BridgeInterface;
import cc.test.bridge.TransactionInterface;
import cc.test.bridge.BridgeConstants.State;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Transaction extends Entry implements TransactionInterface
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	protected final String repositoryName = "Transactions";
	protected Integer transactionId = -1;
	protected String name = "N/A";
	protected Double amount = 0.0;
	protected Date date = new Date();
	protected Integer parentLineId = -1;
	
	public Transaction()
	{
	}
	
	public Transaction(Integer id)
	{
		this.parentLineId = id;
	}

	@Override
	public Boolean commit()
	{
		Boolean successful = false;
		State state = BridgeHelper.getHamper().get(this);
		 
        if (state != null) {
        	if (state == State.CREATE || state == State.UPDATE){
        		try
				{
					MongoHelper.insert(this, "Transactions", "cc");
				} catch (Exception e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
        		if(state == State.CREATE){
        			for(BridgeInterface bridge : BridgeHelper.getHamper().keySet()) {
        				if(bridge instanceof Line){
        					if(((Line) bridge).getId() == this.parentLineId){
        						((Line) bridge).transactionIds.add(this.id);
        					}
        				}
        			}
        		}
        	}
            
            else if (state == State.DELETE){
            	Mongo mongo = null;
          		try
          		{
          			mongo = new Mongo();
          		} catch (UnknownHostException e)
          		{
          			// TODO Auto-generated catch block
          			e.printStackTrace();
          		} catch (MongoException e)
          		{
          			// TODO Auto-generated catch block
          			e.printStackTrace();
          		}
          		
                  DB db = mongo.getDB("cc");
                  DBCollection collection = db.getCollection(this.getRepositoryName());
                  collection.remove(new BasicDBObject("entry.id", this.getId()));
        		for(BridgeInterface bridge : BridgeHelper.getHamper().keySet()) {
    				if(((Line) bridge).getId() == this.parentLineId){
    					((Line) bridge).sublineIds.remove(this.id);
    				}
    			}
            }  
        }
        

        return successful;
	}

	@Override
	public String getName()
	{
		return this.name;
	}

	@Override
	public String getRepositoryName()
	{
		return null;
	}

	@Override
	public void setName(String arg0)
	{
		this.name = arg0;
		BridgeHelper.getHamper().put(this, State.UPDATE);
	}

	@Override
	public Double getAmount()
	{
		return this.amount;
	}

	@Override
	public Date getDate()
	{
		return this.date;
	}

	@Override
	public void setAmount(Double arg0)
	{
		this.amount = arg0;
		BridgeHelper.getHamper().put(this, State.UPDATE);	
	}

	@Override
	public void setDate(Date arg0)
	{
		this.date = arg0;
		
	}
	
	public Integer getTransactionId()
	{
		return transactionId;
	}

	public void setTransactionId(Integer transactionId)
	{
		this.transactionId = transactionId;
		BridgeHelper.getHamper().put(this, State.UPDATE);
	}

	public Integer getParentLineId()
	{
		return parentLineId;
	}

	public void setParentLineId(Integer parentLineId)
	{
		this.parentLineId = parentLineId;
		BridgeHelper.getHamper().put(this, State.UPDATE);
	}

	public static long getSerialversionuid()
	{
		return serialVersionUID;
	}
}
